package com.atguigu.spring.tx;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTransactionTest {
	
	ApplicationContext ctx = null;
	BookShopDao bookShopDao = null;
	BookShopService bookShopService = null;
			
	{
		ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		bookShopDao = ctx.getBean(BookShopDao.class);
		bookShopService = (BookShopService)ctx.getBean("bookShopService");
	}
	
	@Test
	public void testBookShopService(){
		String username = "张三";
		String isbn = "1001";
		bookShopService.purchase(username, isbn);
	}

	//根据书号获取书的单价
	@Test
	public void testFindBookPriceByIsbn() {
		int price = bookShopDao.findBookPriceByIsbn("1001");
		System.out.println(price);
	}

	//更新书的库存，使书号对应的库存 -1
	@Test
	public void testUpdateBookStock() {
		bookShopDao.updateBookStock("1001");
	}

	//更新用户的账户余额： 使username 的balance - price
	@Test
	public void testUpdateUserAccount() {
		bookShopDao.updateUserAccount("张三", 100);
	}

	@Test
	public void test() throws SQLException{
		DataSource dataSource = (DataSource)ctx.getBean("dataSource");
		System.out.println(dataSource.getConnection());
	}
	
}
